<template>
  <div>
    <a-drawer :title="obj.title" :width="570" placement="right" :visible="visible" @close="visible = false">
      <a-row class="title">
        {{ obj.subTitle }}
      </a-row>
      <a-form>
        <a-form-item label="分组名称" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
          <a-input v-model="obj.groupName" @keypress.enter="saveData" />
        </a-form-item>
      </a-form>
      <div class="divs">
        <a-button class="btn_bag" :style="{ marginRight: '8px' }" @click="saveData">
          保存
        </a-button>
        <a-button v-if="obj.title && obj.title.startsWith('修改')" type="danger" :style="{ marginRight: '8px' }" @click="deleteGroup(obj)">
          删除
        </a-button>
        <a-button @click="visible = false">取消</a-button>
      </div>
    </a-drawer>
  </div>
</template>

<script>
import { Project, ProjectApi } from "@/api/project/new-index";
export default {
  inject: ["reload"],

  props: {
    newGroupOptions: {
      show: false,
      obj: {}
    }
  },
  data() {
    return {
      visible: false,
      obj: {
        moduleType: "hr",
        groupName: "",
        groupDesc: "",
        companyId: this.$store.state.hr.companyId
      }
    };
  },

  watch: {
    newGroupOptions: {
      handler(val) {
        this.visible = val.show;
        val.obj.moduleType = "hr";
        this.obj = val.obj;
      },
      deep: true
    }
  },

  methods: {
    // 删除分组
    deleteGroup(item) {
      this.$confirm({
        title: "确定删除?",
        okText: "确定",
        okType: "danger",
        cancelText: "取消",
        onOk: () => {
          Project({
            url: ProjectApi.postGroupList + `/${item.id}`,
            method: "DELETE"
          }).then(data => {
            if (data.success) {
              this.$notification["success"]({
                message: "删除成功"
              });
              this.$parent.getGroupList();
              this.reload();
            }
          });
        }
      });
    },
    saveData() {
      if (this.obj.id) {
        Project({
          url: ProjectApi.postGroupList,
          method: "PUT",
          data: this.obj
        }).then(data => {
          if (data.success) {
            this.$notification["success"]({
              message: "修改成功"
            });
            /*  this.$parent.getGroupList() */
            this.reload();
          }
        });
      } else {
        Project({
          url: ProjectApi.postGroupList,
          method: "POST",
          data: this.obj
        }).then(data => {
          if (data.success) {
            this.$notification["success"]({
              message: "新增成功"
            });
            /* this.$parent.getGroupList() */
            this.reload();
          }
        });
      }
      this.visible = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.title {
  padding: 16px 20px;
  background: #fafafa;
  margin-bottom: 10px;
}
.divs {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e9e9e9;
  padding: 10px 16px;
  background: #fff;
  text-align: center;
}
</style>
